<template>
  <div class="app-container">
    <el-tabs v-model="queryParams.machineType" @tab-click="handleChangeTab">
      <el-tab-pane label="内部农机" name="0" />
      <el-tab-pane label="外部农机" name="1" />
    </el-tabs>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="carNumber">
        <el-input
          v-model="queryParams.carNumber"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆类型" prop="carType">
        <el-select v-model="queryParams.carType" placeholder="请选择车辆类型" clearable>
          <el-option
            v-for="dict in dict.type.without_car_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="机主" prop="carOwner">
        <el-input
          v-model="queryParams.carOwner"
          placeholder="请输入机主"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机手" prop="driver">
        <el-input
          v-model="queryParams.driver"
          placeholder="请输入机手"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="用户权限" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户权限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门权限" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门权限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:machinery:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:machinery:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:machinery:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:machinery:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="machineryList" @selection-change="handleSelectionChange" style="overflow-x: scroll;">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键自增" align="center" prop="id" /> -->
      <el-table-column label="序号">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="carNumber" width="250"/>
      <el-table-column label="车长" align="center" prop="vehicleCommanderName" v-if="queryParams.machineType === '0'"/>
      <el-table-column label="车主" align="center" prop="carOwner" v-if="queryParams.machineType === '1'"/>
      <el-table-column label="机手" align="center" :prop="queryParams.machineType === '0'? 'userName' : 'driver'" />
      <!-- dlCarTypeName  carType-->
      <el-table-column label="车辆类型" align="center" prop="carType" width="250">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.without_car_type" :value="scope.row.carTypeName"/>
        </template>
      </el-table-column>
      <el-table-column label="可进行作业" align="center" prop="" width="200"/>
      <el-table-column label="所属单位" align="center" prop="orgName" v-if="queryParams.machineType === '0'" width="400"/>
      <el-table-column label="品牌" align="center" :prop="queryParams.machineType === '0'? 'brandName' : 'carBrand'" >
      </el-table-column>
      <el-table-column label="型号" align="center" :prop="queryParams.machineType === '0'?'modelName':'engineType'" />
      <el-table-column label="车架号" align="center" :prop="queryParams.machineType === '0'?'cjh':'carVin'" />
      <el-table-column label="发动机型号" align="center" :prop="queryParams.machineType === '0'?'engineModel':'engineType'" />
      <el-table-column label="功率（马力）" align="center" :prop="queryParams.machineType === '0'?'power':'enginePower'" />
      <el-table-column label="设备名称" align="center" prop="carName" v-if="queryParams.machineType === '0'"/>
      <!-- 可进行作业，还缺 -->
      <!-- <el-table-column label="部门权限" align="center" prop="deptId" /> -->
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="300">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="relateHomeWorkProject(scope.row)"
            v-if="queryParams.machineType === '0'"
            v-hasPermi="['system:machinery:edit']"
          >关联作业项目</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-if="queryParams.machineType === '1'"
            v-hasPermi="['system:machinery:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-if="queryParams.machineType === '1'"
            v-hasPermi="['system:machinery:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="detail(scope.row)"
            v-hasPermi="['system:machinery:edit']"
          >详情</el-button>
         
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改外部农机对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车牌号" prop="carNumber">
          <el-input v-model="form.carNumber" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车辆类型" prop="carType">
          <el-select v-model="form.carType" placeholder="请选择车辆类型">
            <el-option
              v-for="dict in dict.type.without_car_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆品牌" prop="carBrand">
          <el-input v-model="form.carBrand" placeholder="请输入车辆品牌" />
        </el-form-item>
        <el-form-item label="车辆型号" prop="carModel">
          <el-input v-model="form.carModel" placeholder="请输入车辆型号" />
        </el-form-item>
        <el-form-item label="车架号" prop="carVin">
          <el-input v-model="form.carVin" placeholder="请输入车架号" />
        </el-form-item>
        <el-form-item label="发动机功率" prop="enginePower">
          <el-input v-model="form.enginePower" placeholder="请输入发动机功率" />
        </el-form-item>
        <el-form-item label="机主" prop="carOwner">
          <el-input v-model="form.carOwner" placeholder="请输入机主" />
        </el-form-item>
        <el-form-item label="机主电话" prop="carOwnerPhone">
          <el-input v-model="form.carOwnerPhone" placeholder="请输入机主电话" />
        </el-form-item>
        <el-form-item label="机主身份证" prop="carOwnerNum">
          <el-input v-model="form.carOwnerNum" placeholder="请输入机主身份证" />
        </el-form-item>
        <el-form-item label="机手" prop="driver">
          <el-input v-model="form.driver" placeholder="请输入机手" />
        </el-form-item>
        <el-form-item label="机手电话" prop="driverPhone">
          <el-input v-model="form.driverPhone" placeholder="请输入机手电话" />
        </el-form-item>
        <el-form-item label="机手身份证" prop="driverNum">
          <el-input v-model="form.driverNum" placeholder="请输入机手身份证" />
        </el-form-item>
        <el-form-item label="行驶证" prop="carLicence">
          <el-input v-model="form.carLicence" placeholder="请输入行驶证" />
        </el-form-item>
        <el-form-item label="驾驶证" prop="drivingLicence">
          <el-input v-model="form.drivingLicence" placeholder="请输入驾驶证" />
        </el-form-item>
        <el-form-item label="扩展字段1" prop="reVar1">
          <el-input v-model="form.reVar1" placeholder="请输入扩展字段1" />
        </el-form-item>
        <el-form-item label="扩展字段2" prop="reVar2">
          <el-input v-model="form.reVar2" placeholder="请输入扩展字段2" />
        </el-form-item>
        <el-form-item label="扩展字段3" prop="reVar3">
          <el-input v-model="form.reVar3" placeholder="请输入扩展字段3" />
        </el-form-item>
        <el-form-item label="扩展字段4" prop="reVar4">
          <el-input v-model="form.reVar4" placeholder="请输入扩展字段4" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="逻辑删除标记" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入逻辑删除标记" />
        </el-form-item>
        <el-form-item label="用户权限" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户权限" />
        </el-form-item>
        <el-form-item label="部门权限" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门权限" />
        </el-form-item>
        <el-divider content-position="center">外部农机附件信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddAgriculturalMachineryAnnex">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteAgriculturalMachineryAnnex">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="agriculturalMachineryAnnexList" :row-class-name="rowAgriculturalMachineryAnnexIndex" @selection-change="handleAgriculturalMachineryAnnexSelectionChange" ref="agriculturalMachineryAnnex">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="文件名称" prop="fileName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fileName" placeholder="请输入文件名称" />
            </template>
          </el-table-column>
          <el-table-column label="文件类型" prop="fileType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.fileType" placeholder="请选择文件类型">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="文件地址" prop="filePath" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.filePath" placeholder="请输入文件地址" />
            </template>
          </el-table-column>
          <el-table-column label="上传人" prop="uploadBy" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.uploadBy" placeholder="请输入上传人" />
            </template>
          </el-table-column>
          <el-table-column label="上传时间" prop="uploadTime" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.uploadTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择上传时间" />
            </template>
          </el-table-column>
          <el-table-column label="扩展字段1" prop="reVar1" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.reVar1" placeholder="请输入扩展字段1" />
            </template>
          </el-table-column>
          <el-table-column label="扩展字段2" prop="reVar2" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.reVar2" placeholder="请输入扩展字段2" />
            </template>
          </el-table-column>
          <el-table-column label="扩展字段3" prop="reVar3" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.reVar3" placeholder="请输入扩展字段3" />
            </template>
          </el-table-column>
          <el-table-column label="扩展字段4" prop="reVar4" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.reVar4" placeholder="请输入扩展字段4" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMachinery, getMachinery, delMachinery, addMachinery, updateMachinery, listOuterMachinery, listInnerMachinery } from "@/api/system/machinery";

export default {
  name: "Machinery",
  dicts: ['without_car_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedAgriculturalMachineryAnnex: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外部农机表格数据
      machineryList: [],
      // 外部农机附件表格数据
      agriculturalMachineryAnnexList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNumber: null,
        carType: null,
        carOwner: null,
        driver: null,
        userId: null,
        deptId: null,
        // 内外部农机
        machineType: '0', //0为内部农机，1为外部农机
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // 这里有待优化，默认初始获取内部农机
    this.getInnerList();
    this.getList();
  },
  methods: {
    /** 查询外部农机列表 */
    getList() {
      // this.loading = true;
    if(this.queryParams.machineType === '0'){
      this.getInnerList();
    }else if(this.queryParams.machineType === '1'){
      this.getOuterList();
    }
      // listMachinery(this.queryParams).then(response => {
      //   this.machineryList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },

    getOuterList() {
      this.loading = true;
      listOuterMachinery(this.queryParams).then(response => {
        this.machineryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getInnerList() {
      this.loading = true;
      listInnerMachinery(this.queryParams).then(response => {
        this.machineryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carNumber: null,
        carType: null,
        carBrand: null,
        carModel: null,
        carVin: null,
        engineType: null,
        enginePower: null,
        carOwner: null,
        carOwnerPhone: null,
        carOwnerNum: null,
        driver: null,
        driverPhone: null,
        driverNum: null,
        carLicence: null,
        drivingLicence: null,
        reVar1: null,
        reVar2: null,
        reVar3: null,
        reVar4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        userId: null,
        deptId: null
      };
      this.agriculturalMachineryAnnexList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加外部农机";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMachinery(id).then(response => {
        this.form = response.data;
        this.agriculturalMachineryAnnexList = response.data.agriculturalMachineryAnnexList;
        this.open = true;
        this.title = "修改外部农机";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.agriculturalMachineryAnnexList = this.agriculturalMachineryAnnexList;
          if (this.form.id != null) {
            updateMachinery(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMachinery(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除外部农机编号为"' + ids + '"的数据项？').then(function() {
        return delMachinery(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 外部农机附件序号 */
    rowAgriculturalMachineryAnnexIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 外部农机附件添加按钮操作 */
    handleAddAgriculturalMachineryAnnex() {
      let obj = {};
      obj.fileName = "";
      obj.fileType = "";
      obj.filePath = "";
      obj.uploadBy = "";
      obj.uploadTime = "";
      obj.reVar1 = "";
      obj.reVar2 = "";
      obj.reVar3 = "";
      obj.reVar4 = "";
      this.agriculturalMachineryAnnexList.push(obj);
    },
    /** 外部农机附件删除按钮操作 */
    handleDeleteAgriculturalMachineryAnnex() {
      if (this.checkedAgriculturalMachineryAnnex.length == 0) {
        this.$modal.msgError("请先选择要删除的外部农机附件数据");
      } else {
        const agriculturalMachineryAnnexList = this.agriculturalMachineryAnnexList;
        const checkedAgriculturalMachineryAnnex = this.checkedAgriculturalMachineryAnnex;
        this.agriculturalMachineryAnnexList = agriculturalMachineryAnnexList.filter(function(item) {
          return checkedAgriculturalMachineryAnnex.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleAgriculturalMachineryAnnexSelectionChange(selection) {
      this.checkedAgriculturalMachineryAnnex = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/machinery/export', {
        ...this.queryParams
      }, `machinery_${new Date().getTime()}.xlsx`)
    },
    // 查询按钮
    handleChangeTab(tab) {
      this.queryParams.machineType = tab.name;
      // this.handleQuery();
      this.queryParams.pageNum = 1;
      if(tab.name === '0'){
        this.getInnerList();
      }else{
        this.getOuterList();
      }
    },

    // 关联作业项目
    relateHomeWorkProject(){
      alert("关联作业详情按钮")
    },
    // 详情
    detail(){
      alert("详情")
    }
  }
};
</script>
